package com.example.dragablelistview;

import sample4.dnd.DragListView;
import sample4.provider.MyDatabase.MyTable;
import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;

public class MainActivity extends ListActivity implements
		LoaderCallbacks<Cursor> {
	private MyCursorAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getContentResolver().delete(MyTable.CONTENT_URI, null, null);
		ContentValues values = new ContentValues();
		for (int i = 0; i < 20; i++) {
			values.clear();
			values.put(MyTable.Columns.NAME, "name_" + i);
			values.put(MyTable.Columns.NAME, i);
			getContentResolver().insert(MyTable.CONTENT_URI, values);
		}

		DragListView list = (DragListView) findViewById(android.R.id.list);
		mAdapter = new MyCursorAdapter(this,
				android.R.layout.simple_list_item_1, null, true);
		list.setAdapter(mAdapter);
		list.setDragEnable(true);

		getLoaderManager().initLoader(0, null, this);

	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new CursorLoader(getApplicationContext(), MyTable.CONTENT_URI,
				null, null, null, MyTable.Columns.ORDER);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor newCursor) {
		mAdapter.swapCursor(newCursor);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		mAdapter.swapCursor(null);
	}
}
